<?php

/**
 * @file
 * Definition of example_handler_custom_field
 */

/**
 * Provides a custom views field.
 */
class jessica_stylebook_handler_style_item extends views_handler_field {
    function option_definition() {
        $options = parent::option_definition();
        return $options;
    }

    function options_form(&$form, &$form_state) {
        parent::options_form($form, $form_state);
    }

    function query() {
        // do nothing -- to override the parent query.
    }

    function render($data) {
        //dsm($data, 'data');

        $nid = $data->nid;

        $query = 'SELECT
            sid.field_styling_item_id_target_id as nid,	
	        xpin.field_pin_x_coordinate_value as xpin,
	        ypin.field_pin_y_coordinate_value as ypin,
	        dir.field_pin_direction_value as dir
        FROM
           field_data_field_styling_item as si
        LEFT JOIN
            field_data_field_pin_show_on_thumbnail tn
        ON
            (tn.entity_id = si.field_styling_item_value)
        LEFT JOIN
            field_data_field_styling_item_id sid
        ON
            (sid.entity_id = si.field_styling_item_value)
        LEFT JOIN
            field_data_field_pin_x_coordinate xpin
        ON
            (xpin.entity_id = si.field_styling_item_value)
        LEFT JOIN
            field_data_field_pin_y_coordinate ypin
        ON
            (ypin.entity_id = si.field_styling_item_value)
        LEFT JOIN
            field_data_field_pin_direction dir
        ON
            (dir.entity_id = si.field_styling_item_value)			
            
        WHERE
            tn.field_pin_show_on_thumbnail_value = 1
        AND
            si.entity_id = :nid';

        $result = db_query($query, array(':nid' => $data->nid));

        $item = null;

        foreach ($result as $record) {
            $item = $record;
        }

        $item_node = null;

        if ($item !== null) {
            $item_node = node_load($item->nid);
        }

        $json = array();

        if ($item_node) {
            $brand = taxonomy_term_load($item_node->field_item_brand['und'][0]['tid']);
            $price = empty($item_node->field_item_price['und'][0]['value']) ? '' : $item_node->field_item_price['und'][0]['value'];

            //prefix $ sign
            if ($price != '') {
                $price = '$' . $price;
            }

            $thumbnail = image_style_url('related_styling_article_thumbnail', $item_node->field_item_image['und'][0]['uri']);

            $buy = NULL;

            if (!empty($item_node->field_item_buy_link)) {
                $buy = $item_node->field_item_buy_link['und'][0]['safe_value'];
            }

            $instock = FALSE;

            if (!empty($item_node->field_item_in_stock)) {
                if ($item_node->field_item_in_stock['und'][0]['value'] == 1) {
                    $instock = TRUE;
                }
                elseif ($item_node->field_item_in_stock['und'][0]['value'] == 0) {
                    $instock = FALSE;
                }
            }

            $json['url'] = base_path() . drupal_get_path_alias('node/' . $item_node->nid);
            $json['brand'] = $brand->name;
            $json['price'] = $price;
            $json['thumbnail'] = $thumbnail;
            $json['x'] = $item->xpin;
            $json['y'] = $item->ypin;
            $json['direction'] = $item->dir;
            $json['buy'] = $buy;
            $json['instock'] = $instock;
        }

        /*
        $items = $data->field_field_styling_item;

        $json = array();

        foreach ($items as $key => $item) {
            //dpm($item, 'item');

            $fc_item = reset($item['rendered']['entity']['field_collection_item']);
            if (isset($fc_item['field_styling_item_id']) && !empty($fc_item['field_styling_item_id'])) {
                $item_entity = $fc_item['field_styling_item_id']['#items'][0]['entity'];
                $fc_entity = $fc_item['#entity'];

                //dpm($fc_entity, '$fc_entity');
                //dpm($item_entity);

                if ($item_entity->status == 1) {

                    if ($fc_entity->field_pin_show_on_thumbnail['und'][0]['value']) {
                        //dpm($item_entity, '$item_entity');

                        $brand = taxonomy_term_load($item_entity->field_item_brand['und'][0]['tid']);
                        $price = empty($item_entity->field_item_price['und'][0]['value']) ? '' : $item_entity->field_item_price['und'][0]['value'];

                        //prefix $ sign
                        if ($price != '') {
                            $price = '$' . $price;
                        }

                        $thumbnail = image_style_url('related_styling_article_thumbnail', $item_entity->field_item_image['und'][0]['uri']);

                        $buy = NULL;

                        if (!empty($item_entity->field_item_buy_link)) {
                            $buy = $item_entity->field_item_buy_link['und'][0]['safe_value'];
                        }

                        $instock = FALSE;

                        if (!empty($item_entity->field_item_in_stock)) {
                            if ($item_entity->field_item_in_stock['und'][0]['value'] == 1) {
                                $instock = TRUE;
                            }
                            elseif ($item_entity->field_item_in_stock['und'][0]['value'] == 0) {
                                $instock = FALSE;
                            }
                        }

                        $json['url'] = base_path() . drupal_get_path_alias('node/' . $item_entity->nid);
                        $json['brand'] = $brand->name;
                        $json['price'] = $price;
                        $json['thumbnail'] = $thumbnail;
                        $json['x'] = $fc_entity->field_pin_x_coordinate['und'][0]['value'];
                        $json['y'] = $fc_entity->field_pin_y_coordinate['und'][0]['value'];
                        $json['direction'] = $fc_entity->field_pin_direction['und'][0]['value'];
                        $json['buy'] = $buy;
                        $json['instock'] = $instock;
                    }
                    break;
                }
            }
        } */

        //dpm($json, '$json');

        $json_base64 = base64_encode(json_encode($json, JSON_FORCE_OBJECT));

        $output = '<div class="item_data" data="' . $json_base64 . '"></div>';

        $output .= '<!--' . json_encode($json, JSON_PRETTY_PRINT) . '-->';

        return $output;
    }
}

